package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.consts.GlobalConsts;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.common.constant.MessageQueueTagConsts;
import com.zmn.oms.common.dio.wallquotation.WallQuotationDIO;
import com.zmn.oms.model.entity.wallquotation.WallQuotation;
import com.zmn.oms.services.interfaces.wallquotation.WallQuotationService;
import com.zmn.oms.zmn.business.interfaces.wallquotation.WallQuotationBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 墙面报价侦听器
 *
 * @author chengguang
 * @date 2022/08/03
 */
@Slf4j
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class WallQuotationListener {


    @Resource
    private WallQuotationBService wallQuotationBService;

    @Resource
    private WallQuotationService wallQuotationService;

    /**
     * 保存未成单数据
     *
     * @param message JSON字符串
     * @return
     * @author liudi
     * @since: 2022/1/18 15:10
     */
    @AliyunMQHandler(tags = MessageQueueTagConsts.WALL_ORDER_AUTOMATIC_CONFIRMATION_TAG)
    public void confirmWallQuotation(String message) {
        try {
            WallQuotationDIO wallQuotationDIO = JSON.parseObject(message, WallQuotationDIO.class);
            WallQuotation wallQuotation = wallQuotationService.findById(wallQuotationDIO.getWorkId());
            if (Objects.equals(wallQuotation.getWallStatus(), GlobalConsts.YES)) {
                return;
            }
            wallQuotationDIO.setAutomaticConfirmation(true);
            log.info("#MQ#WallQuotationListener#confirmWallQuotation wallQuotationDIO:{}", JSON.toJSONString(wallQuotationDIO));
            wallQuotationBService.confirmWallQuotation(wallQuotationDIO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("#MQ#WallQuotationListener#confirmWallQuotation 发生异常：{}", e.getMessage(), e);
        }
    }
}
